<?php
/**
 * 来店管控周数据
 * @author qianxun
 *
 */
class reachWeekModel extends MY_Model{
    function reachWeekModel(){
        parent::MY_Model();
        $this->table = 'ReachWeek';
    }
    /**
     * 包装数据
     * {@inheritDoc}
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        //时段配置列表
        $this->load->model('timeUnitModel');
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        foreach ($list as $k => $v){
            //时段名称
            $v['timeUnitStr'] = isset($timeUnitDataList[$v['year']][$v['timeUnit']]) ? $timeUnitDataList[$v['year']][$v['timeUnit']]['name'] : '';
            //周名称
            $v['weekNumStr'] = isset($timeUnitDataList[$v['year']][$v['timeUnit']]['weekList'][$v['weekNum']]) ? $timeUnitDataList[$v['year']][$v['timeUnit']]['weekList'][$v['weekNum']] : '';
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 更新父类金额
     * {@inheritDoc}
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->load->model('reachTimeUnitModel');
        $this->reachTimeUnitModel->updateMoney($parentId);
    }
    /**
     * 获取父级ID
     * {@inheritDoc}
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        return isset($data['reachTimeUnitId']) ? (int)$data['reachTimeUnitId'] : 0;
    }
    /**
     * 获取子类金额
     * @param number $reachTimeUnitId
     * @return number[]|unknown[]
     */
    function getMoney($reachTimeUnitId){
        $findList = $this->getList(array('reachTimeUnitId'=>$reachTimeUnitId));
        $data = array(
            'comeStoreActual' => 0,//来店实际
            'pickCarActual' => 0,//提车实际
            'deliveryActual' => 0,//交车实际
        );
        foreach ($findList as $v){
            $data['comeStoreActual'] += $v['comeStoreActual'];
            $data['pickCarActual'] += $v['pickCarActual'];
            $data['deliveryActual'] += $v['deliveryActual'];
        }
        return $data;
    }
    /**
     * 更新计算
     * @param number $id
     */
    function updateMoney($id){
        $findData = $this->getData($id);
        $this->load->model('reachTimeUnitModel');
        $reachTimeUnitData = $this->reachTimeUnitModel->getData($findData['reachTimeUnitId']);
        $data = array();
        //来店目标达成率
        $data['comeStoreRate'] = (int)$reachTimeUnitData['comeStoreTarget'] ? ($findData['comeStoreActual'] / $reachTimeUnitData['comeStoreTarget']) * 100 : 0;
        //转换率
        $data['transRate'] = (int)$findData['comeStoreActual'] ? ($findData['deliveryActual'] / $findData['comeStoreActual']) * 100 : 0;
        $this->edit($id, $data);
    }
    /**
     * 获取专营店周列表
     * @param number $storeId
     * @param number $year
     * @return array
     */
    function getStoreListByWeek($storeId,$year){
        $findList = $this->getNewList(array('storeId'=>$storeId,'year'=>$year));
        $list = array();
        foreach ($findList as $k => $v){
            $list[$v['timeUnit']][$v['weekNum']] = $v;
        }
        return $list;
    }
    /**
     * 获取周列表
     * @param mixed $where
     * @return array
     */
    function combineList($timeUnitDataList = array()){
        $reachTimeUnitIds = array();
        $newTimeUnitDataList = array();
        //重新组装双月列表
        foreach ($timeUnitDataList as $v){
            $newTimeUnitDataList[(int)$v['id']] = $v;
        }
        $reachTimeUnitIds = array_keys($newTimeUnitDataList);
        
        $findList = $this->getNewList(array('reachTimeUnitId'=>array('in', $reachTimeUnitIds)));
        $list = array();
        foreach ($findList as $k => $v){
            isset($newTimeUnitDataList[$v['reachTimeUnitId']]['weekDataList']) || $newTimeUnitDataList[$v['reachTimeUnitId']]['weekDataList'] = array();
            $newTimeUnitDataList[$v['reachTimeUnitId']]['weekDataList'][$v['weekNum']] = $v;
        }
        return $newTimeUnitDataList;
    }
}